package cn.tedu.tmall.front.mall.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.front.mall.dao.cache.ICartCacheRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IGoodsRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IOrderItemRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IOrderRepository;
import cn.tedu.tmall.front.mall.dao.persist.repository.IReceiverAddressRepository;
import cn.tedu.tmall.front.mall.pojo.entity.Order;
import cn.tedu.tmall.front.mall.pojo.entity.OrderItem;
import cn.tedu.tmall.front.mall.pojo.param.OrderAddNewParam;
import cn.tedu.tmall.front.mall.pojo.po.CartPO;
import cn.tedu.tmall.front.mall.pojo.vo.GoodsStandardVO;
import cn.tedu.tmall.front.mall.pojo.vo.OrderListItemVO;
import cn.tedu.tmall.front.mall.pojo.vo.OrderStandardVO;
import cn.tedu.tmall.front.mall.pojo.vo.ReceiverAddressStandardVO;
import cn.tedu.tmall.front.mall.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 处理订单数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IOrderRepository orderRepository;
    @Autowired
    private IOrderItemRepository orderItemRepository;
    @Autowired
    private IReceiverAddressRepository receiverAddressRepository;
    @Autowired
    private IGoodsRepository goodsRepository;
    @Autowired
    private ICartCacheRepository cartCacheRepository;

    public OrderServiceImpl() {
        log.debug("创建业务类对象：OrderServiceImpl");
    }

    @Override
    public Long create(CurrentPrincipal currentPrincipal, OrderAddNewParam orderAddNewParam) {
        log.debug("开始处理【创建订单】的业务，当事人：{}，参数：{}", currentPrincipal, orderAddNewParam);
        Long receiverAddressId = orderAddNewParam.getReceiverAddressId();
        ReceiverAddressStandardVO receiverAddress = receiverAddressRepository.getStandardById(receiverAddressId);
        if (receiverAddress == null) {
            String message = "创建订单失败，收货地址数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        if (!receiverAddress.getUserId().equals(currentPrincipal.getId())) {
            String message = "创建订单失败，收货地址数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        String fullAddress = receiverAddress.getProvince()
                + receiverAddress.getCity()
                + receiverAddress.getArea()
                + receiverAddress.getDetail();

        Long[] goodsIdList = orderAddNewParam.getGoodsIdList();

        BigDecimal totalPrice = BigDecimal.ZERO;
        Integer goodsTotalNum = 0;

        List<GoodsStandardVO> goodsList = new ArrayList<>();
        List<Integer> goodsNumList = new ArrayList<>();
        for (Long goodsId : goodsIdList) {
            GoodsStandardVO goods = goodsRepository.getStandardById(goodsId);
            if (goods == null) {
                String message = "创建订单失败，部分商品数据不存在！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
            }
            if (goods.getIsPutOn() != 1) {
                String message = "创建订单失败，商品【" + goods.getTitle() + "】已下架！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }

            // 你可能需要检查商品的库存（本项目未涉及此概念）

            goodsList.add(goods);

            CartPO cartPO = cartCacheRepository.getByUserAndGoods(currentPrincipal.getId(), goodsId);
            if (cartPO == null) {
                String message = "创建订单失败，购物车数据异常，请返回购物车并重新下单！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
            CartPO.CartInfo cartInfo = cartPO.getCartInfo();
            if (cartInfo == null) {
                String message = "创建订单失败，购物车数据异常，请返回购物车并重新下单！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
            Integer goodsNum = cartInfo.getGoodsNum();
            goodsNumList.add(goodsNum);

            goodsTotalNum += goodsNum;

            BigDecimal salePrice = goods.getSalePrice();
            BigDecimal multiply = salePrice.multiply(new BigDecimal(goodsNum));
            totalPrice = totalPrice.add(multiply);
        }

        System.out.println(goodsList);
        System.out.println(goodsNumList);

        Order order = new Order();
        order.setOrderNo(UUID.randomUUID().toString());
        order.setBuyerId(currentPrincipal.getId());
        order.setBuyerUsername(currentPrincipal.getUsername());
        order.setReceiverName(receiverAddress.getReceiverName());
        order.setReceiverPhone(receiverAddress.getReceiverPhone());
        order.setReceiverAddress(fullAddress);
        order.setGoodsNum(goodsTotalNum);
        order.setTotalPrice(totalPrice);
        order.setOrderState(ORDER_STATE_UNPAID);
        int rows = orderRepository.insert(order);
        if (rows != 1) {
            String message = "创建订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        for (int i = 0; i < goodsList.size(); i++) {
            GoodsStandardVO goods = goodsList.get(i);
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setGoodsId(goods.getId());
            orderItem.setGoodsCoverUrl(goods.getCoverUrl());
            orderItem.setGoodsTitle(goods.getTitle());
            orderItem.setGoodsNum(goodsNumList.get(i));
            orderItem.setSaleUnitPrice(goods.getSalePrice());
            rows = orderItemRepository.insert(orderItem);
            if (rows != 1) {
                String message = "创建订单失败，服务器忙，请稍后再试！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_INSERT, message);
            }
        }

        for (Long goodsId : goodsIdList) {
            cartCacheRepository.deleteByUserAndGoods(currentPrincipal.getId(), goodsId);
        }

        return order.getId();
    }

    @Override
    public OrderStandardVO getStandardById(CurrentPrincipal currentPrincipal, Long orderId) {
        log.debug("开始处理【根据ID查询订单】的业务，当事人：{}，参数：{}", currentPrincipal, orderId);
        OrderStandardVO order = orderRepository.getStandardByIdAndUser(orderId, currentPrincipal.getId());
        if (order == null) {
            String message = "查询订单失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return order;
    }

    @Override
    public PageData<OrderListItemVO> listByUser(CurrentPrincipal currentPrincipal, Integer pageNum) {
        log.debug("开始处理【查询订单列表】的业务，用当事人户：{}, 页码：{}", currentPrincipal, pageNum);
        return orderRepository.listByUser(currentPrincipal.getId(), pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<OrderListItemVO> listByUser(CurrentPrincipal currentPrincipal, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询订单列表】的业务，当事人：{}, 页码：{}，每页记录数：{}", currentPrincipal, pageNum, pageSize);
        return orderRepository.listByUser(currentPrincipal.getId(), pageNum, pageSize);
    }

}
